package com.org.manhu.chargehelper.custom;

import java.util.ArrayList;
import java.util.List;

import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.tools.LoadImage;
import com.org.manhu.chargehelper.tools.StringUtils;

import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.PopupWindow;


public class ImageBigPopwindow extends PopupWindow {

	ViewPager viewPager;
	ArrayList<View> list;
	ViewGroup main, group;
	ImageView imageView;
	ImageView[] imageViews;

	public ImageBigPopwindow(final Activity context, List<String> imageList) {

		LayoutInflater inflater = context.getLayoutInflater();
		list = new ArrayList<View>();
		ImageView iv = null;
		for (int i = 0; i < imageList.size(); i++) {
			iv = new ImageView(context);
			LoadImage.LoadPic(StringUtils.formatPicture(imageList.get(i)), iv);
			iv.setAdjustViewBounds(true);
			iv.setScaleType(ScaleType.CENTER_CROP);
			list.add(iv);
			iv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					hidePopupWindow();
				}
			});
		}

		imageViews = new ImageView[list.size()];
		ViewGroup main = (ViewGroup) inflater.inflate(R.layout.fragment_image,
				null);
		// group是R.layou.main中的负责包裹小圆点的LinearLayout.
		ViewGroup group = (ViewGroup) main.findViewById(R.id.viewGroup);

		viewPager = (ViewPager) main.findViewById(R.id.viewPager);
		

		for (int i = 0; i < list.size(); i++) {
			imageView = new ImageView(context);
			imageView.setLayoutParams(new LayoutParams(30, 16));
			imageView.setPadding(14, 0, 0, 0);
			imageViews[i] = imageView;
			if (i == 0) {
				// 默认进入程序后第一张图片被选中;
				imageViews[i].setImageResource(R.drawable.guide_dot_white);
			} else {
				imageViews[i].setImageResource(R.drawable.guide_dot_black);
			}
			group.addView(imageView);
		}

		viewPager.setAdapter(new MyAdapter());
		viewPager.setOnPageChangeListener(new MyListener());

		Rect frame = new Rect();
		context.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);

		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(main);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(h);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(01);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimationPreview);

	}

	class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			return super.getItemPosition(object);
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			// TODO Auto-generated method stub
			((ViewPager) arg0).removeView(list.get(arg1));
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			// TODO Auto-generated method stub
			((ViewPager) arg0).addView(list.get(arg1));
			return list.get(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public Parcelable saveState() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void finishUpdate(View arg0) {
			// TODO Auto-generated method stub

		}
	}

	/**
	 * @文件名：ImageBigPopwindow.java
	 * @包名：com.org.manhu.custom
	 * @作者：李朋辉            @QQ：892839211
	 * @创建时间：2015年1月7日   下午5:30:36、
	 *
	 * @描述：viewpager的监听
	 */
	class MyListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			currentdot(arg0);

		}

	}

	/**
	 * @描述：切换图片
	 * @param imgecurrent
	 */
	public void currentimage(int imgecurrent) {
		viewPager.setCurrentItem(imgecurrent);
		currentdot(imgecurrent);
	}

	/**
	 * @描述：切换指示器
	 * @param dotNum
	 */
	public void currentdot(int dotNum) {
		for (int i = 0; i < imageViews.length; i++) {
			imageViews[dotNum]
					.setImageResource(R.drawable.guide_dot_white);
			if (dotNum != i) {
				imageViews[i].setImageResource(R.drawable.guide_dot_black);
			}
		}
	}

	/**
	 * @描述：隐藏popupwindow
	 */
	public void hidePopupWindow() {
		if (this.isShowing()) {
			this.dismiss();
		}
	}

	public void showPopupWindow(View view) {
		if (!this.isShowing()) {
			// lineView.setVisibility(View.VISIBLE);
			// this.showPopupWindow(view);
			this.showAtLocation(view, Gravity.CENTER, 0, 0);
		} else {
			// lineView.setVisibility(View.GONE);
			this.dismiss();
		}
	}
}
